<?php

	/**
	 * @see Zend_Controller_Action_Helper_ContextSwitch
	 */
	require_once 'Zend/Controller/Action/Helper/ContextSwitch.php';
	
	/**
	 * @see Bike_Format
	 */
	require_once 'Bike/Format.php';
	
	
	class Bike_Controller_Action_Helper_ContextSwitch extends Zend_Controller_Action_Helper_ContextSwitch
	{
	    public function initContext($format = null)
	    {
	        $this->_currentContext = null;

	        $controller = $this->getActionController();
	        $request    = $this->getRequest();
	        $action     = $request->getActionName();

	        // Return if no context switching enabled, or no context switching
	        // enabled for this action
	        $contexts = $this->getActionContexts($action);
	        if (empty($contexts)) 
	        {
	            return;
	        }

	        // Return if no context parameter provided
	        if (!$context = $request->getParam($this->getContextParam())) 
	        {
	            if ($format === null) 
	            {
	                return;
	            }
	            $context = $format;
	            $format  = null;
	        }

	        if($context == Bike_Format::getInstance()->getDefault())
	        {
	        	return;
	        }

	        // Check if context allowed by action controller
	        if (!$this->hasActionContext($action, $context)) {
	            return;
	        }

	        // Return if invalid context parameter provided and no format or invalid
	        // format provided
	        if (!$this->hasContext($context)) {
	            if (empty($format) || !$this->hasContext($format)) {

	                return;
	            }
	        }

	        // Use provided format if passed
	        if (!empty($format) && $this->hasContext($format)) {
	            $context = $format;
	        }

	        $suffix = $this->getSuffix($context);

	        $this->_getViewRenderer()->setViewSuffix($suffix);

	        $headers = $this->getHeaders($context);
	        if (!empty($headers)) {
	            $response = $this->getResponse();
	            foreach ($headers as $header => $content) {
	                $response->setHeader($header, $content);
	            }
	        }

	        if ($this->getAutoDisableLayout()) {
	            /**
	             * @see Zend_Layout
	             */
	            require_once 'Zend/Layout.php';
	            $layout = Zend_Layout::getMvcInstance();
	            if (null !== $layout) {
	                $layout->disableLayout();
	            }
	        }

	        if (null !== ($callback = $this->getCallback($context, self::TRIGGER_INIT))) {
	            if (is_string($callback) && method_exists($this, $callback)) {
	                $this->$callback();
	            } elseif (is_string($callback) && function_exists($callback)) {
	                $callback();
	            } elseif (is_array($callback)) {
	                call_user_func($callback);
	            } else {
	                /**
	                 * @see Zend_Controller_Action_Exception
	                 */
	                require_once 'Zend/Controller/Action/Exception.php';
	                throw new Zend_Controller_Action_Exception(sprintf('Invalid context callback registered for context "%s"', $context));
	            }
	        }

	        $this->_currentContext = $context;
	    }


	}

